﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DAO;


namespace TMDT_Nhom1A.UserControl
{
    public partial class DangNhap : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (Membership.ValidateUser(Login.UserName, Login.Password + Login.UserName))
            {
                string Role = "";
                string UserID = "";
                Customer_BUS.GetRoleOfUser(Login.UserName, ref UserID, ref Role);
                Session["UserName"] = Login.UserName;
                Session["Role"] = Role;
                Session["UserID"] = UserID;
                e.Authenticated = true;
            }
            else
            {
                Login.FailureText = "Đăng nhập thất bại";

            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Session["Role"].ToString() == "")
            {
                logoutPanel.Visible = false;
                loginPanel.Visible = true;
                triggerButton.Text = "Đăng nhập";
            }
            else
            {
                lbvaitro.Text = Session["Role"].ToString();
                lbtaikhoan.Text = Session["UserName"].ToString();
                logoutPanel.Visible = true;
                loginPanel.Visible = false;
                lbvaitro.ForeColor = System.Drawing.Color.Red;
                triggerButton.Text = "Đăng xuất";
            }
        }

        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            e.Cancel = false;
            Session["Role"] = "";
            Session["UserName"] = "";
            Session["UserID"] = "";
            Session["Flag"] = 0;
            Session["Cart"] = new Cart();
        }
    }
}